//
//  BookSettingViewController.m
//  FunRead
//
//  Created by  King on 15/5/22.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "BookSettingViewController.h"
#import "SoftwareIntroduce.h"
#import "SettingsCell.h"
#import "DeleteCache.h"
@interface BookSettingViewController ()<UITableViewDataSource, UITableViewDelegate, SettingsCellDelegate>

@property(nonatomic, retain)UITableView *table;

@end

@implementation BookSettingViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
    }
    return self;
}

- (void)dealloc
{
    [_table release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    
    UIImage *image = [[UIImage imageNamed:@"set.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"书城" image:image tag:2];
    self.tabBarItem = item;
    
    self.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height );
    self.navigationItem.title = @"系统设置";
    

    [self createTable];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.table reloadData];
}


#pragma mark -
#pragma mark - 创建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, self.view.frame.size.height - 49 - 64) style:UITableViewStylePlain];
    [self.table setBackgroundColor:[UIColor whiteColor]];
    self.table.rowHeight = 50;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    // 隐藏滑动条
    self.table.showsVerticalScrollIndicator = NO;
    // 开始设置headertable
    
    // 设置tableview的footer
    self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    //静止来回晃动
        self.table.bounces = NO;
    [self.table release];
}


#pragma mark - 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            SoftwareIntroduce *soft = [[SoftwareIntroduce alloc]init];
            soft.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:soft animated:YES];
            [soft release];
            
        }
        if (indexPath.row == 1) {
            // 清楚缓存
            dispatch_async(
                           dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                           , ^{
                               NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                               
                               NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                               NSLog(@"files :%ld",(unsigned long)[files count]);
                               
                               for (NSString *p in files)
                               {
                                   NSError *error;
                                   NSString *path = [cachPath stringByAppendingPathComponent:p];
                                   if ([[NSFileManager defaultManager] fileExistsAtPath:path])
                                   {
                                       [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                                   }
                               }
                               [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
                           }
                           );

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.detailTextLabel.text = @"0KB";
        }
    }
}

#pragma mark -
#pragma mark - required协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *cellIndentity = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity];
        
        }
        // 点击不变色
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        // 在后面加大于号
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"关于软件";
                
            }
            if (indexPath.row == 1) {
                cell.textLabel.text = @"清除缓存";
                // 缓存大小
                 NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                CGFloat num = [DeleteCache folderSizeAtPath:cachPath];
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fKB", num];
                
                
            }
//            if (indexPath.row == 2) {
//                
//                cell.textLabel.text = @"给我打分";
//                
//            }
//            
//            if (indexPath.row == 3) {
//                
//                
//            }
            
            
        }
            return cell;
        
    }
    else{
       
        static NSString *cellIndentity1 = @"cell1";
        SettingsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentity1];
        
        if (!cell) {
            cell = [[SettingsCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentity1];
        }
        
        cell.delegate = self;
        // 点击不变色
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.swi.indentify = indexPath.row;
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"防止休眠"; // 三个滑动的
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"信息默认更新";
        }
//        if (indexPath.row == 2) {
//            
//        
//            cell.textLabel.text = @"在线阅读";
//        }
//        if (indexPath.row == 3) {
//            cell.textLabel.text = @"精品应用";
//        }
    }
        return cell;
    }
}

// 协议实现
- (void)Test1:(NSInteger)num
{
    if (num == 0) {
        
        // 不休眠
        [self showAlert:@"不休眠"];
        
    }
    if (num == 1) {
        NSLog(@"信息默认更新");
        
            }
//    if (num == 2) {
//        NSLog(@"2shi");
//    }
}

- (void)clearCacheSuccess
{
    [self showAlert:@"成功清理"];
    
}

#pragma mark - 输出框
- (void)showAlert:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(TimeFireMethod:) userInfo:alert repeats:YES];
    [alert show];
    
}

- (void)TimeFireMethod:(NSTimer *)timer
{
    UIAlertView *alert = (UIAlertView *)timer.userInfo;
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    alert  = NULL;
    
    
}
- (void)Test2:(NSInteger)num
{
    if (num == 0) {
        NSLog(@"0不shi");
        [self showAlert:@"休眠"];
    }
    if (num == 1) {
        NSLog(@"1不shi");
    }
//    if (num == 2) {
//        NSLog(@"2不shi");
//    }

}
#pragma mark - 返回分区数
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return 2;
}
#pragma mark - 返回标题数
#pragma mark - 标题
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"系统基础设置";
    }
    else
    {
        return @"阅读功能设置";
        
    }
//    UIResponder
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
